/**
 * 执行终端命令相关的代码
 */
const { spawn } = require('child_process');

// npm install
const commandSpawn = async (command, args, options = {}) => {
  try {
    const childProcess = spawn(command, args, { ...options, shell: true });
    childProcess.stdout.pipe(process.stdout);
    childProcess.stderr.pipe(process.stderr);

    return new Promise((resolve, reject) => {
      childProcess.on("close", (code) => {
        if (code === 0) {
          resolve(code);
        } else {
          reject(new Error(`Process exited with code ${code}`));
        }
      });
    });
  } catch (error) {
    return Promise.reject(error);
  }
}

module.exports = {
  commandSpawn
}